unit CommonLib;

interface

uses Graphics, SysUtils, Classes, WinSock;

function StringToFont(const S: string; Font: TFont): boolean;
function FontToString(Font: TFont): string;
function GetLocalIPAddress: string;

implementation

function GetLocalIPAddress: string;
type
  PU_Long = ^U_Long;
var
  WSData: TWSAData;
  HostEnt: PHostEnt;
  InAddr: TInAddr;
  NameBuf: array[byte] of AnsiChar;
begin
  if WSAStartup($101, WSData) <> 0 then
    Exit('0.0.0.0');

  gethostname(namebuf, SizeOf(NameBuf));
  HostEnt := gethostbyname(namebuf);
  InAddr.S_addr := U_Long(PU_Long(HostEnt.h_addr_list^)^);

  Result := inet_ntoa(InAddr);
  WSACleanup;
end;

function FontToString(Font: TFont): string;
var
  sColor, sStyle: string;
begin
  if assigned(Font) then
  begin
    sColor := '$' + IntToHex(ColorToRGB(Font.Color), 8);
    sStyle := IntToStr(byte(Font.Style));

    Result := Font.Name + '|' + IntToStr(Font.Size) + '|' + sColor + '|' +
      sStyle;
  end;
end;

function StringToFont(const S: string; Font: TFont): boolean;
var
  afont: TFont;
  Strs:  TStringList;
begin
  afont := TFont.Create;
  try
    afont.Assign(Font);
    Strs := TStringList.Create;
    try
      Strs.Text := StringReplace(S, '|', #10, [rfReplaceAll]);
      Result    := Strs.Count = 4;
      if Result then
        begin
          afont.Name  := Strs[0];
          afont.Size  := StrToInt(Strs[1]);
          afont.Color := StrToInt(Strs[2]);
          afont.Style := TFontStyles(byte(StrToInt(Strs[3])));
        end;
      Font.Assign(afont);
    finally
      Strs.Free;
    end;
  finally
    afont.Free;
  end;
end;


end.
